import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'

// 布局组件
const Layout = () => import('@/layout/index.vue')

// 基础路由
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index.vue'),
    meta: {
      title: '登录',
      hidden: true
    }
  },
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/error/404.vue'),
    meta: {
      title: '404',
      hidden: true
    }
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index.vue'),
        meta: {
          title: '首页',
          icon: 'House',
          affix: true
        }
      }
    ]
  }
]

// 动态路由
export const asyncRoutes: RouteRecordRaw[] = [
  // 财务管理
  {
    path: '/finance',
    component: Layout,
    redirect: '/finance/data-filter',
    name: 'Finance',
    meta: {
      title: '财务管理',
      icon: 'Money',
      roles: ['admin', 'finance']
    },
    children: [
      {
        path: 'data-filter',
        name: 'DataFilter',
        component: () => import('@/views/finance/data-filter/index.vue'),
        meta: {
          title: '数据筛选',
          icon: 'Filter'
        }
      },
      {
        path: 'channel-analysis',
        name: 'ChannelAnalysis',
        component: () => import('@/views/finance/channel-analysis/index.vue'),
        meta: {
          title: '渠道分析',
          icon: 'TrendCharts'
        }
      },
      {
        path: 'star-analysis',
        name: 'StarAnalysis',
        component: () => import('@/views/finance/star-analysis/index.vue'),
        meta: {
          title: '星级分析',
          icon: 'Star'
        }
      }
    ]
  },
  
  // 综合排行
  {
    path: '/ranking',
    component: Layout,
    redirect: '/ranking/performance',
    name: 'Ranking',
    meta: {
      title: '综合排行',
      icon: 'Trophy',
      roles: ['admin', 'manager']
    },
    children: [
      {
        path: 'performance',
        name: 'PerformanceRanking',
        component: () => import('@/views/ranking/performance/index.vue'),
        meta: {
          title: '业绩排行',
          icon: 'TrendCharts'
        }
      },
      {
        path: 'visit',
        name: 'VisitRanking',
        component: () => import('@/views/ranking/visit/index.vue'),
        meta: {
          title: '上门排行',
          icon: 'Position'
        }
      },
      {
        path: 'workload',
        name: 'WorkloadRanking',
        component: () => import('@/views/ranking/workload/index.vue'),
        meta: {
          title: '工作量排行',
          icon: 'DataAnalysis'
        }
      }
    ]
  },
  
  // 工作审计
  {
    path: '/audit',
    component: Layout,
    children: [
      {
        path: 'index',
        name: 'WorkAudit',
        component: () => import('@/views/audit/index.vue'),
        meta: {
          title: '工作审计',
          icon: 'Document',
          roles: ['admin', 'auditor']
        }
      }
    ]
  },
  
  // 合同管理
  {
    path: '/contract',
    component: Layout,
    redirect: '/contract/team',
    name: 'Contract',
    meta: {
      title: '合同管理',
      icon: 'Document',
      roles: ['admin', 'contract']
    },
    children: [
      {
        path: 'team',
        name: 'TeamContract',
        component: () => import('@/views/contract/team/index.vue'),
        meta: {
          title: '团队合同',
          icon: 'UserFilled'
        }
      },
      {
        path: 'personal',
        name: 'PersonalContract',
        component: () => import('@/views/contract/personal/index.vue'),
        meta: {
          title: '我的合同',
          icon: 'User'
        }
      },
      {
        path: 'certificate',
        name: 'CertificateContract',
        component: () => import('@/views/contract/certificate/index.vue'),
        meta: {
          title: '权证合同',
          icon: 'Stamp'
        }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: constantRoutes,
  scrollBehavior: () => ({ left: 0, top: 0 })
})

export default router
